﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.Client.Common.Session;


#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmDirectoryServiceUsers : Form
    {

        private msgXMLResponse LastList;

        //private bool TreeIsSynchronized = false;
        private string _LogonName;
        private string _ID;
        private int _SecurityProviderID;
        private string _SecurityProviderDescription;
        private XmlNodeList _PropertyMaps;
        private bool _SelectUsers;


        public frmDirectoryServiceUsers( string logonName, string iD, int securityProviderID, string securityProviderDescription, bool SelectUsers )
        {
            _LogonName = logonName;
            _ID = iD;
            _SecurityProviderID = securityProviderID;
            _SecurityProviderDescription = securityProviderDescription;
            _SelectUsers = SelectUsers;
            InitializeComponent();
            txtDirectoryService.Text = securityProviderDescription;
        }

        public string LogonName
        {
            get { return _LogonName; }
        }

        public string ID
        {
            get { return _ID; }
        }

        public XmlNodeList PropertyMaps
        {
            get { return _PropertyMaps; }
        }
        private void frmDirectoryService_Load(object sender, EventArgs e)
        {
            tvwtree.Enabled = false;
            cmdConfirm.Enabled = false;
            tvwtree.Nodes.Clear();
            tvwtree.Enabled = false;

            msgXMLResponse res = BusinessActions.GetDirectoryTree(_SecurityProviderID);
            XmlNode node = res.Document.SelectSingleNode("Root");
            if (node != null)
            {
                TreeNode NewNode = tvwtree.Nodes.Add(node.Attributes.GetNamedItem("Path").Value, node.Attributes.GetNamedItem("Name").Value);
                NewNode.Tag = node.Attributes.GetNamedItem("Expanded").Value;
                LoadNodes(node, NewNode);
            }
            tvwtree.Enabled = true;
            tvwtree.Select();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();

        }


        private void LoadNodes(XmlNode node, TreeNode tvwNode)
        {
            TreeNode NewNode;
            foreach (XmlNode nod in node.ChildNodes)
            {
                NewNode = tvwNode.Nodes.Add(nod.Attributes.GetNamedItem("Path").Value, nod.Attributes.GetNamedItem("Name").Value );
                NewNode.Tag = nod.Attributes.GetNamedItem("Expanded").Value;
                if (nod.HasChildNodes)
                    LoadNodes(nod, NewNode);
            }
        }

        private void tvwtree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                lbxUsers.Items.Clear();
                msgSecurityUsersListRequest req = new msgSecurityUsersListRequest(_SecurityProviderID, e.Node.Name, "");
                if (_SelectUsers) 
                    LastList =    BusinessActions.GetDirectoryNodeUsers(req);
                else
                    LastList = BusinessActions.GetDirectoryNodeGroups(req);
                XmlNode node = LastList.Document.SelectSingleNode("Root");
                if (node != null)
                {
                    foreach (XmlNode nod in node.ChildNodes)
                    {
                        lbxUsers.Items.Add(nod.Attributes.GetNamedItem("Name").Value);
                    }
                }
            }

        }

        private void tvwtree_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (tvwtree.SelectedNode != null)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void lbxUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxUsers.SelectedIndex > -1)
            {
                cmdConfirm.Enabled = true;
                string s = (string)lbxUsers.SelectedItem;
                _ID = LastList.Document.SelectSingleNode("/Root/Node[@Name='" + s + "']").Attributes.GetNamedItem("ID").Value;
                _LogonName = LastList.Document.SelectSingleNode("/Root/Node[@Name='" + s + "']").Attributes.GetNamedItem("LogonName").Value;
                _PropertyMaps = LastList.Document.SelectSingleNode("/Root/Node[@Name='" + s + "']").ChildNodes;
            }
        }
    }

}